//
//  MyShopDemandCell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/12.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyShopDemandCell.h"
#import "HomeModel.h"
@interface MyShopDemandCell()
@property (weak, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UIImageView *iv;
@property (weak, nonatomic) IBOutlet UILabel *contentL;
@property (weak, nonatomic) IBOutlet UILabel *nickNamedAndIndustry;
@property (weak, nonatomic) IBOutlet UIButton *contactBtn;
@property (weak, nonatomic) IBOutlet UILabel *creatTimeL;
@property (weak, nonatomic) IBOutlet UIView *subView;
@property (weak, nonatomic) IBOutlet UILabel *nickNamedL;
@property (weak, nonatomic) IBOutlet UIButton *indicationBtn;
@property (weak, nonatomic) IBOutlet UIButton *phoneBtn;
@property (weak, nonatomic) IBOutlet UIButton *weiChatBtn;
@property (weak, nonatomic) IBOutlet UIButton *QQBtn;
@property (weak, nonatomic) IBOutlet UIImageView *ivSub;
@property (weak, nonatomic) IBOutlet UIButton *deleBtn;
@property (weak, nonatomic) IBOutlet UIView *btnView;

@end
@implementation MyShopDemandCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.view.layer.masksToBounds = YES;
    self.view.layer.cornerRadius = 3;
    self.view.layer.borderColor = kLineColor.CGColor;
    self.view.layer.borderWidth = 1;
    self.subView.layer.masksToBounds = YES;
    self.subView.layer.cornerRadius = 3;
    self.subView.layer.borderColor = kLineColor.CGColor;
    self.subView.layer.borderWidth = 1;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.btnView.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(3, 3)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.btnView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.btnView.layer.mask = maskLayer;
    self.subView.hidden = YES;
    self.iv.layer.masksToBounds = YES;
    self.iv.layer.cornerRadius = 38/2;
    self.ivSub.layer.masksToBounds = YES;
    self.ivSub.layer.cornerRadius = 38/2;
    self.contactBtn.layer.masksToBounds = YES;
    self.contactBtn.layer.cornerRadius = 3;
    self.contactBtn.layer.borderColor = kLineColor.CGColor;
    self.contactBtn.layer.borderWidth = 1;
    [self.contactBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
    self.phoneBtn.layer.masksToBounds = YES;
    self.phoneBtn.layer.cornerRadius = 3;
    self.phoneBtn.layer.borderColor = kOrangeBg.CGColor;
    self.phoneBtn.layer.borderWidth = 1;
    self.weiChatBtn.layer.masksToBounds = YES;
    self.weiChatBtn.layer.cornerRadius = 3;
    self.weiChatBtn.layer.borderColor = [UIColor colorWithRed:226/255.0 green:237/255.0 blue:209/255.0 alpha:1].CGColor;
    self.weiChatBtn.layer.borderWidth = 1;
    self.weiChatBtn.layer.cornerRadius = 3;
    self.weiChatBtn.layer.borderColor = [UIColor colorWithRed:179/255.0 green:210/255.0 blue:135/255.0 alpha:1].CGColor;
    self.weiChatBtn.layer.borderWidth = 1;
    self.QQBtn.layer.masksToBounds = YES;
    self.QQBtn.layer.cornerRadius = 3;
    self.QQBtn.layer.borderColor = kLineColor.CGColor;
    self.QQBtn.layer.borderWidth = 1;
    self.deleBtn.layer.masksToBounds = YES;
    self.deleBtn.layer.cornerRadius = 3;
    self.onOffLineBtn.layer.masksToBounds = YES;
    self.onOffLineBtn.layer.cornerRadius = 3;
}
-(void)setHomeModel:(HomeModel *)homeModel
{
    _homeModel = homeModel;
    if (homeModel.show) {
        self.view.hidden = YES;
        self.subView.hidden = NO;
    }else {
        self.view.hidden = NO;
        self.subView.hidden = YES;
    }
    self.btnView.hidden = self.view.hidden;
    [self.iv sd_setImageWithURL:[NSURL URLWithString:homeModel.headImg] placeholderImage:[UIImage imageNamed:@"headImg"]];
    NSString *string = @"";
    if (homeModel.nicakName.length) {
        string = [NSString stringWithFormat:@"%@|%@",homeModel.nicakName,homeModel.industryList[0]];
    }else {
        string = [NSString stringWithFormat:@"%@|%@",[UserDefaultsHelper readIphone],homeModel.industryList[0]];
    }
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
    NSString *subString = [NSString stringWithFormat:@"|%@",homeModel.industryList[0]];
    [text addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:105/255.0 green:105/255.0 blue:105/255.0 alpha:1]range:[string rangeOfString:subString]];
    self.nickNamedAndIndustry.attributedText = text;
    NSTimeInterval interval    = homeModel.creattime/1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [formatter stringFromDate: date];
    self.creatTimeL.text = currentDateStr;
    self.contentL.text = [NSString stringWithFormat:@"需求概述:%@",homeModel.title];
    XLog(@"homeModel.represent:%@",homeModel.represent);
    
    
    XLog(@"homemodel:%@",homeModel.associationtypesubList);
    XLog(@"homeModel.associationtypesubList.count:%ld",homeModel.associationtypesubList.count);
    [self.ivSub sd_setImageWithURL:[NSURL URLWithString:homeModel.headImg] placeholderImage:[UIImage imageNamed:@"headImg"]];
    self.nickNamedL.text = homeModel.nicakName;
    XLog(@"self.nickNamedL.text:%@",self.nickNamedL.text);
    
    if (homeModel.phone.length == 0) {
        [self.phoneBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        self.phoneBtn.layer.borderColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1].CGColor;
        [self.phoneBtn setTitle:[NSString stringWithFormat:@" 电话:该客户暂未填写"] forState:UIControlStateNormal];
    }else{
        [self.phoneBtn setTitle:[NSString stringWithFormat:@" 电话:%@",homeModel.phone] forState:UIControlStateNormal];
        self.phoneBtn.layer.borderColor = [UIColor colorWithRed:242/255.0 green:176/255.0 blue:81/255.0 alpha:1].CGColor;
        
        
    }
    if (homeModel.wechat.length == 0) {
        [self.weiChatBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        self.weiChatBtn.layer.borderColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:0.5].CGColor;
        [self.weiChatBtn setTitle:[NSString stringWithFormat:@" 微信:该客户暂未填写"] forState:UIControlStateNormal];
    }else{
        [self.weiChatBtn setTitle:[NSString stringWithFormat:@" 微信:%@",homeModel.wechat] forState:UIControlStateNormal];
        self.weiChatBtn.layer.borderColor = [UIColor colorWithRed:151/255.0 green:193/255.0 blue:91/255.0 alpha:1].CGColor;
    }
    if (homeModel.qq.length == 0) {
        [self.QQBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        self.QQBtn.layer.borderColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:0.5].CGColor;
        [self.QQBtn setTitle:[NSString stringWithFormat:@" QQ:该客户暂未填写"] forState:UIControlStateNormal];
    }else{
        [self.QQBtn setTitle:[NSString stringWithFormat:@" QQ:%@",homeModel.qq] forState:UIControlStateNormal];
        self.QQBtn.layer.borderColor = [UIColor colorWithRed:194/255.0 green:194/255.0 blue:194/255.0 alpha:1].CGColor;
        
    }
    
    
}
- (IBAction)contactBtn:(UIButton *)sender {
    
    sender.selected = YES;
    self.indicationBtn.selected = NO;
    //    sender.selected = NO;
    if ([self.delegate respondsToSelector:@selector(myShopDemandCell:withClickBtn:withIndexPath:)]) {
        [self.delegate myShopDemandCell:self withClickBtn:sender withIndexPath:self.indexPath];
    }
}
- (IBAction)indicatorBtn:(UIButton *)sender {
    
    sender.selected = NO;
    self.contactBtn.selected = NO;
    if ([self.delegate respondsToSelector:@selector(myShopDemandCell:withClickBtn:withIndexPath:)]) {
        [self.delegate myShopDemandCell:self withClickBtn:sender withIndexPath:self.indexPath];
    }
}

- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
}
- (IBAction)deleBtn:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(myShopDemandCell:withClickBtn:withIndexPath:)]) {
        [self.delegate myShopDemandCell:self withClickBtn:sender withIndexPath:self.indexPath];
    }
}
- (IBAction)onOffLineBtnBtn:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(myShopDemandCell:withClickBtn:withIndexPath:)]) {
        [self.delegate myShopDemandCell:self withClickBtn:sender withIndexPath:self.indexPath];
    }
}
//- (IBAction)changeBtn:(UIButton *)sender {
//    if ([self.delegate respondsToSelector:@selector(myShopDemandCell:withClickBtn:withIndexPath:)]) {
//        [self.delegate myShopDemandCell:self withClickBtn:sender withIndexPath:self.indexPath];
//    }
//}
- (IBAction)phoneBtn:(UIButton *)sender {
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"不可以给自己打电话"];
}
- (IBAction)weiChatBtn:(UIButton *)sender {
    
    if (self.homeModel.wechat.length != 0) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = [sender currentTitle];
        NSURL *url = [NSURL URLWithString:@"weixin://"];
        BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:url];
        if (canOpen) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"不可以联系自己"];
            
        }else{
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请安装微信"];
        }
    }else {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"该客户暂未填写微信号,无法联系"];
    }
    
}
- (IBAction)qqBtn:(UIButton *)sender {
    
    if (self.homeModel.qq.length != 0) {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"不可以联系自己"];
        }else{
            XLog(@"对不起，您还未安装QQ");
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请安装QQ"];
        }
    }else {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"该客户暂未填写QQ号,无法联系"];
    }
    
}
@end
